package com.jingxi.portal.controller;

import com.jingxi.common.pojo.JingXiResult;
import com.jingxi.portal.pojo.CartItem;
import com.jingxi.portal.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    @RequestMapping("/add/{itemId}")
    public String addCartItem(@PathVariable Long itemId,
                              @RequestParam(defaultValue = "1") Integer num,
                              HttpServletRequest request,
                              HttpServletResponse response)throws IOException {
        JingXiResult result=cartService.addCartItem(itemId,num,request,response);
        return "redirect:/cart/success.html";
    }
    @RequestMapping("/success")
    public String showSuccess(){
        return "cartSuccess";
    }
    @RequestMapping("/cart")
    public String showCart(HttpServletResponse response, HttpServletRequest request, Model model){
        List<CartItem> list=cartService.getCartItemList(response,request);
        model.addAttribute("cartList",list);
        return "cart";
    }
    @RequestMapping("/delete/{itemId}")
    public String deleteCartItem(@PathVariable Long itemId,HttpServletRequest request,HttpServletResponse response){
        cartService.deleteCartItem(itemId,response,request);
        return "redirect:/cart/cart";
    }
}
